<?php

// 初期設定
if (!defined("IN_APP")) {
	define("IN_APP", true);
	include_once("../../include/init.php");
	requireLogin();
}

// ログインしていなければ、終了してエラーメッセージを出力
loginRequired();

//　タブ別の設定の読み込み
require_once("tab.include.php");

// ページ名
$page_name = "group.tab";

/******************* ページの処理　*******************/

class Group {
	var $id, $name, $leader;
}

$groups = array();

$connect = connectToDb();

$query = "SELECT groups.group_id,title,owner_id,member.del_flag FROM group_members AS member ";
$query .= "INNER JOIN groups ON member.group_id = groups.group_id ";
$query .= "WHERE user_id = '" . mysql_real_escape_string($_SESSION['user_id']) . "' ";
$query .= "AND (member.del_flag <> 'd' OR member.del_flag IS NULL)";

$result = mysql_query($query, $connect);

$user_id = $_SESSION['user_id'];

$user_is_leader = false;

if ( !$result ) {
	exit("Invalid query: $query - " . mysql_error());
}
else {
	while ($row = mysql_fetch_assoc($result)) {
		$group = new Group();
		$group->id = $row["group_id"];
		$group->name = $row["title"];
		
		if (isset($_SESSION['group'])) {
			if ($group->id == $_SESSION['group']) {
				$group->current = true;
			}
			else {
				$group->current = false;
			}
		}
		else {
			$group->current = false;
		}
		
		if ($row["owner_id"] == $user_id) {
			$user_is_leader = true;
			$group->leader = true;
		}
		else
			$group->leader = false;
		
		array_push($groups, $group);
	}
}

mysql_close($connect);

/******************** 処理終了 ********************/

//　ページを表示する
$include_path = $SETTINGS['VIEW_PATH'] . $tab_name . "/" . $page_name . $SETTINGS['VIEW_NAME'];
include($include_path);

?>	